package com.jiuzhe.commercial.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.bean.AccountListBean;

import java.util.List;

/**
 * Created by huanghao on 2018/4/27.
 */

public class AccountInfoAdapter extends RecyclerView.Adapter<AccountInfoAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<AccountListBean> list;
    private OnItemClickListener mOnItemClickListener = null;

    public AccountInfoAdapter(List<AccountListBean> list) {
        this.list = list;
    }

    public  interface OnItemClickListener {
        void onItemClick(View view, int position);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_account_item2, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        v.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final AccountListBean houseListBean = list.get(position);
        holder.itemView.setTag(position);
        holder.tvSkuName.setText(houseListBean.getSkuName());
        holder.tvLocation.setText(houseListBean.getAddress());
        holder.tvTime.setText(houseListBean.getCreateTime());
    }


    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvSkuName;
        TextView tvLocation;
        TextView tvTime,tvAccount;


        public ViewHolder(View v) {
            super(v);
            tvSkuName = v.findViewById(R.id.tv_skuName);
            tvLocation = v.findViewById(R.id.tv_location);
            tvTime = v.findViewById(R.id.tv_time);
            tvAccount = v.findViewById(R.id.tv_account);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
